package classes.controlers {
	import classes.managers.Game_Manager;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Kihoonee
	 */
	public class Carrot_Controler {
		
		private var _mouseEnable:Boolean;
		private var gm:Game_Manager;
		private var _parent:MovieClip;
		private var mc:MovieClip;
		
		private var _shadowHit:Boolean = false;
		
		public function Carrot_Controler( _gm:Game_Manager, _mc:MovieClip ) {
			gm = _gm;
			mc = _mc;
			_parent = mc.parent as MovieClip;
		}
		
		public function init():void {
			addEvent();
		}
		
		private function addEvent():void {
			mc.addEventListener(MouseEvent.ROLL_OVER, ROLL_OVER );
			mc.addEventListener(MouseEvent.ROLL_OUT, ROLL_OUT );
			mc.addEventListener(MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			mc.cursor_mc.addEventListener(Event.ENTER_FRAME, cursorMove );
		}
		
		private function ROLL_OUT(e:MouseEvent):void {
			mc.cursor_mc.visible = false;
		}
		
		private function ROLL_OVER(e:MouseEvent):void {
			mc.cursor_mc.visible = true;
		}
		
		private function cursorMove(e:Event):void {
			mc.cursor_mc.x = mc.mouseX;
			mc.cursor_mc.y = mc.mouseY;
		}
		
		public function MOUSE_DOWN(e:MouseEvent = null ):void {
			mc.arrow_mc.visible = false;
			mc.cursor_mc.gotoAndStop(2);
			
			mc.addEventListener(MouseEvent.MOUSE_UP, MOUSE_UP );
			mc.addEventListener(Event.ENTER_FRAME, ENTER_FRAME );
		}
		
		private function ENTER_FRAME(e:Event):void {
			
			if ( mc.x < _parent.box_mc.x ) {
				mc.x = _parent.box_mc.x;
			}else if ( mc.x > _parent.box_mc.x + _parent.box_mc.width ) {
				mc.x = _parent.box_mc.x + _parent.box_mc.width;
			}
			
			if ( mc.y < _parent.box_mc.y ) {
				mc.y = _parent.box_mc.y;
			}else if ( mc.y > _parent.box_mc.y + _parent.box_mc.height ) {
				mc.y = _parent.box_mc.y + _parent.box_mc.height;
			}
			
			if ( shadowHit ) {
				//mc.x = _parent.mouseX;
				mc.y = _parent.mouseY;
			}else {
				if ( mc.hitArea_mc.hitTestObject( _parent.carrotShadow_mc.hitArea_mc ) ) {
					mc.x = _parent.carrotShadow_mc.x;
					mc.y = _parent.carrotShadow_mc.y;
					_parent.carrotShadow_mc.visible = false;
					shadowHit = true;
				}else {
					mc.x = _parent.mouseX;
					mc.y = _parent.mouseY;
				}
			}
		}
		
		private function MOUSE_UP(e:MouseEvent):void {
			mc.arrow_mc.visible = true;
			mc.cursor_mc.gotoAndStop(1);
			
			_parent.carrotShadow_mc.visible = true;
			gm.asYouWere();
			
			mc.removeEventListener(MouseEvent.MOUSE_UP, MOUSE_UP );
			mc.removeEventListener(Event.ENTER_FRAME, ENTER_FRAME );
		}
		
		public function removeEvent():void {
			mc.cursor_mc.visible = false;
			mc.cursor_mc.gotoAndStop(1);
			
			shadowHit = false;
			if ( mc.hasEventListener(MouseEvent.MOUSE_DOWN) ) mc.removeEventListener(MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
			
			if ( mc.hasEventListener(MouseEvent.MOUSE_UP) ) mc.removeEventListener(MouseEvent.MOUSE_UP, MOUSE_UP );
			
			if ( mc.hasEventListener(Event.ENTER_FRAME) ) mc.removeEventListener(Event.ENTER_FRAME, ENTER_FRAME );
			
			if ( mc.cursor_mc.hasEventListener(Event.ENTER_FRAME) ) mc.cursor_mc.removeEventListener(Event.ENTER_FRAME, cursorMove );
			if ( mc.hasEventListener(MouseEvent.ROLL_OVER) ) mc.removeEventListener(MouseEvent.ROLL_OVER, ROLL_OVER );
			if ( mc.hasEventListener(MouseEvent.ROLL_OUT) ) mc.removeEventListener(MouseEvent.ROLL_OUT, ROLL_OUT );
			
		}
		
		public function get mouseEnable():Boolean {
			return _mouseEnable;
		}
		
		public function set mouseEnable(value:Boolean):void {
			_mouseEnable = value;
			mc.mouseEnabled = value;
		}
		
		public function get shadowHit():Boolean {
			return _shadowHit;
		}
		
		public function set shadowHit(value:Boolean):void {
			_shadowHit = value;
		}
	}
}